package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_config.MalayAsiaApplication;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.VerifyActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.VerifyActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_ui.login.activity.LoginUserActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.SendEmailDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedList;

import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2018/12/3.
 * 邮箱/短信验证界面
 */
public class VerifyActivity extends BaseMvpActivity<
        VerifyActivityView, VerifyActivityPresenter> implements VerifyActivityView {
    private String resetLoginPassEmail;
    private String resetLoginPassMobile;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verify;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        resetLoginPassEmail = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL);
        resetLoginPassMobile = getIntent().getStringExtra(CommonConstant.RESET_LOGIN_PASS_MOBILE);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @OnClick({
            R.id.ll_verify_email,
            R.id.ll_verify_mobile
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_verify_email:
                getPresenter().sendEmail(resetLoginPassEmail);
                break;
            case R.id.ll_verify_mobile:
                Intent intent = new Intent(this, VerifyMobileActivity.class);
                intent.putExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL, resetLoginPassEmail);
                intent.putExtra(CommonConstant.RESET_LOGIN_PASS_MOBILE, resetLoginPassMobile);
                startActivity(intent);
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeProfileEvent(ChangeProfileEvent event) {
        if (event != null && event.changeProfileType == ChangeProfileType.ChangeLoginPass) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void sendEmailResult(ResultModel resultModel, String email) {
        if (resultModel.isSuccess()) {
            SendEmailDialog sendEmailDialog = new SendEmailDialog(this, email);
            sendEmailDialog.show();
            sendEmailDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    try {
                        LinkedList<Activity> activityLinkedList = MalayAsiaApplication.activityLinkedList;
                        for (Activity activity : activityLinkedList) {
                            if (activity.getClass().getSimpleName().contains("ProfileActivity")) {
                                startActivity(new Intent(mActivity, ProfileActivity.class));
                                break;
                            } else if (activity.getClass().getSimpleName().contains("LoginUserActivity")) {
                                startActivity(new Intent(mActivity, LoginUserActivity.class));
                                break;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }
            });
        }
    }
}
